package eu.goldenak.ircbot;

import java.util.Iterator;
import java.util.Vector;

/**
 * A BotGroup is the place where multiple bots can be stored at once. The bots
 * that are in the BotGroup can then be used to distribute messages for example.
 * This will most likely be used in combination with multi-server support.
 */
public class BotGroup implements Iterable<IrcBot> {
	/**
	 * The bots in this BotGroup.
	 */
	private Vector <IrcBot> m_vBots;
	
	/**
	 * The next index used for distributing.
	 */
	private int m_nBotIndex;
	
	/**
	 * The constructor initializes the properties. Nothing more.
	 */
	public BotGroup () {
		this (3);
	}
	
	/**
	 * Define the beginsize of the IrcBot Vector for optimalizations using this
	 * constructor.
	 */
	public BotGroup (int nBots) {
		m_vBots     = new Vector <IrcBot> (nBots);
		m_nBotIndex = 0;
	}
	
	/**
	 * Adds a pointer to a bot to this BotGroup.
	 */
	public void addBot (IrcBot pBot) {
		m_vBots.add (pBot);
	}
	
	/**
	 * Removes a bot from the BotGroup.
	 */
	public void removeBot (IrcBot pBot) {
		m_vBots.remove (pBot);
	}
	
	/**
	 * Returns the number of bots in this BotGroup.
	 */
	public int numBots () {
		return m_vBots.size ();
	}
	
	/**
	 * Returns an iterator for the bots in this BotGroup.
	 */
	public Iterator <IrcBot> iterator () {
		return m_vBots.iterator ();
	}
	
	/**
	 * Returns a BotGroup with all bots on the specified server.
	 */
	public BotGroup getBotsOnServer (String sServerName) {
		BotGroup pBots = new BotGroup ();
		
		for (IrcBot pBot : m_vBots) {
			if (pBot.getServerName ().equals (sServerName)) {
				pBots.addBot (pBot);
			}
		}
		
		return pBots;
	}
	
	/**
	 * Returns a BotGroup with all bots in the specified channel. I recommend
	 * using this after using getBotsOnServer(), since there might be a bot in
	 * a channel with the same name on a different IRC server.
	 */
	public BotGroup getBotsInChannel (String sChannel) {
		BotGroup pBots = new BotGroup ();
		
		for (IrcBot pBot : m_vBots) {
			if (pBot.isInChannel (sChannel)) {
				pBots.addBot (pBot);
			}
		}
		
		return pBots;
	}
	
	/**
	 * This method allows one to automatically distribute a message evenly over
	 * the bots in this BotGroup.
	 */
	public void privmsg (DistributeMessage pMessage) {
		m_vBots.get (m_nBotIndex ++).out.privmsg (pMessage);
		
		if (m_nBotIndex == m_vBots.size ()) {
			m_nBotIndex = 0;
		}
	}
	
	/**
	 * This class will be used to define message that need to be distributed 
	 * over all connected bots evenly.
	 */
	public static class DistributeMessage {
		/**
		 * The channel where this message should be going.
		 */
		public String channel;
		
		/**
		 * And of course, the message itself.
		 */
		public String message;
		
		/**
		 * The constructor for easy creation.
		 */
		public DistributeMessage (String sChannel, String sMessage) {
			channel = sChannel;
			message = sMessage;
		}
	}
}